/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.web;

import org.fyx.secure.web.*;
import java.util.Observable;
import java.util.Observer;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.fyx.core.FyxRole;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.core.web.event.MenuCreatingEventHandler;
import org.fyx.core.web.event.RoleCreatingEventHandler;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.secure.entity.SecureRole;
import org.fyx.secure.ejb.SecureLocal;
import org.fyx.secure.ejb.SecureRoles;

/**
 *
 * @author xljan
 */
@Stateless
@LocalBean
public class FyxObserver implements Observer {

    @EJB
    private SecureLocal secure;
    @EJB
    private LinkerLocal linker;
    @EJB
    private MulaLocal mula;

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof MenuCreatingEventHandler) {
            menuCreate();
        } else if (o instanceof RoleCreatingEventHandler) {
            RoleCreate();
        }
    }

    private void menuCreate() {
        linker.addMenuItem(mula.get("fyx_system_atributes"),
                linker.getLink("fyx/atributes.xhtml"),
                "fyx_system_atributes", "ui-icon ui-icon-key",
                FyxRole.FYX_SYSTEM_ADMIN);
    }

    private void RoleCreate() {
        SecureRole role = new SecureRole();
        role.setCode(FyxRole.INSTALL_MANAGER);
        role.setTitle("Install module manager");
        role.setDescription("Can install and uninstall modules in system.");
        role.setCategory("System");
        secure.addRole(role);
        
        role = new SecureRole();
        role.setCode(FyxRole.FYX_SYSTEM_ADMIN);
        role.setTitle("System admin");
        role.setDescription("Can manage system atributes and settings.");
        role.setCategory("System");
        secure.addRole(role);

    }
}
